<?php

class Admin_VehiclesController extends Model3_Scaffold_Controller
{

    public function init()
    {
        if (!Model3_Auth::isAuth())
        {
            $this->redirect('Index/index');
        }
    }

    public function __construct($request)
    {
        $this->_sc = new Scaffold_DefaultDb_Vehicle();
        parent::__construct($request);
    }

    public function indexAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        $vehiclesRepository = $em->getRepository('DefaultDb_Entities_Vehicle');
        $this->view->whereAmI = array();
        $this->view->setTemplate('Responsive-3.0');
        $this->view->vehicles = $vehiclesRepository->findAll();
        $this->view->title = 'Vehículos';
        $this->view->description = 'Administración de vehículos';
    }

    public function addAction()
    {
        $breadcrumb = array('Vehículos' => $this->view->url(array('action' => 'index')),);
        $this->view->setTemplate('Responsive-3.0');
        $this->view->whereAmI = $breadcrumb;
        $this->view->title = 'Agregar vehículo';
        $this->view->description = 'Administración de vehículos';
        parent::addAction();
    }

    public function editAction()
    {
        $breadcrumb = array('Vehículos' => $this->view->url(array('action' => 'index')),);
        $this->view->whereAmI = $breadcrumb;
        $this->view->setTemplate('Responsive-3.0');
        $this->view->title = 'Editar vehículo';
        $this->view->description = 'Administración de vehículos';
        parent::editAction();
    }

    public function deleteAction()
    {
        $breadcrumb = array('Vehículos' => $this->view->url(array('action' => 'index')),);
        $this->view->whereAmI = $breadcrumb;
        $this->view->setTemplate('Responsive-3.0');
        $this->view->title = 'Eliminar vehículo';
        $this->view->description = 'Administración de vehículos';
        parent::deleteAction();
    }

    /**
     * Acción que se encarga de asignar un controlador de operaciones a un vehículo.
     */
    public function assignAction()
    {
        $request = $this->getRequest();
        if ($request->isPost() === true)
        {
            $params = $request->getPost();
            $this->assign($params);
            $this->redirect('Admin/Vehicles/index');
            return;
        }
        $vehicleId = $request->getParam('id');
        if ($vehicleId !== null)
        {
            $em = $this->getEntityManager('DefaultDb');
            $vehicleRepository = $em->getRepository('DefaultDb_Entities_Vehicle');
            $userRepository = $em->getRepository('DefaultDb_Entities_User');
            $vehicle = $vehicleRepository->find($vehicleId);
            if ($vehicle instanceof DefaultDb_Entities_Vehicle)
            {
                $controllers = $userRepository->getActiveOperationControllers();
                $breadcrumb = array('Vehículos' => array('action' => 'index'),);
                $this->view->whereAmI = $breadcrumb;
                $this->view->vehicle = $vehicle;
                $this->view->controllers = $controllers;
                $this->view->setTemplate('Responsive-3.0');
                $this->view->title = 'Asignar vehículo a controlador de operaciones';
                $this->view->description = 'Administración de vehículos';
                return;
            }
        }
        $this->redirect('Admin/Vehicles/index');
    }

    /**
     * Asignamos el controlador de operaciones al vehiculo
     * @param array $params Arreglo con los parametros por post. Normalmente $_POST
     */
    private function assign(array $params)
    {
        $em = $this->getEntityManager('DefaultDb');
        $logVehicleDriverRepository = $em->getRepository('DefaultDb_Entities_LogVehicleDriver');
        $vehicleRepository = $em->getRepository('DefaultDb_Entities_Vehicle');
        $userRepository = $em->getRepository('DefaultDb_Entities_User');

        if (isset($params['controller']) === true && isset($params['vehicle']) === true)
        {
            $controllerId = $params['controller'];
            $vehicleId = $params['vehicle'];
            $userSessionId = Model3_Auth::getCredentials('id');
            $user = $userRepository->find($userSessionId);
            $controller = $userRepository->find($controllerId);
            $vehicle = $vehicleRepository->find($vehicleId);
            if ($user instanceof DefaultDb_Entities_User &&
                    $controller instanceof DefaultDb_Entities_User &&
                    $vehicle instanceof DefaultDb_Entities_Vehicle)
            {
                $vehicle->setDriver($controller);
                $logVehicleDriverRepository->addLog($vehicle, $controller, $user);
                $em->persist($vehicle);
                $em->flush();
            }
        }
    }

    /**
     * Esta acción se encarga de mostrar el historial de asignación de un vehículo
     * a constrolador de operaciones o conductores
     */
    function historyAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        $logVehicleDriverRepository = $em->getRepository('DefaultDb_Entities_LogVehicleDriver');
        $vehicleRepository = $em->getRepository('DefaultDb_Entities_Vehicle');

        $request = $this->getRequest();
        $vehicleId = $request->getParam('id');
        if ($vehicleId !== null)
        {
            $vehicle = $vehicleRepository->find($vehicleId);
            if ($vehicle instanceof DefaultDb_Entities_Vehicle)
            {
                $logs = $logVehicleDriverRepository->findBy(array('vehicle' => $vehicleId), array('creationDate' => 'desc'));
                $breadcrumb = array(
                    'Vehículos' => array('action' => 'index'),
                    'Asignar vehículo a controlador de operaciones' => $this->view->url(array('action' => 'assign'), true),
                );
                $this->view->whereAmI = $breadcrumb;
                $this->view->vehicle = $vehicle;
                $this->view->logs = $logs;
                $this->view->setTemplate('Responsive-3.0');
                $this->view->title = 'Historial de asignaciones';
                $this->view->description = 'Administración de vehículos';
                return;
            }
        }
        $this->redirect('Admin/Vehicles/index');
    }

}